package com.jqhee.hjq.ysb;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.Indicators.PagerIndicator;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.daimajia.slider.library.Tricks.ViewPagerEx;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity implements BaseSliderView.OnSliderClickListener {

    private static final String TAG = "MainActivity";
    
    private SliderLayout mSlider;
    private List<BannerInfo> listBanner = new ArrayList<>();
    private PagerIndicator mIndicator;
    private GridView mGridView;

    private int[] icons = new int[] {
            R.drawable.indicator_corner_bg,R.drawable.indicator_corner_bg,
            R.drawable.indicator_corner_bg,R.drawable.indicator_corner_bg,
            R.drawable.indicator_corner_bg, R.drawable.indicator_corner_bg,
            R.drawable.indicator_corner_bg,R.drawable.indicator_corner_bg,
            R.drawable.indicator_corner_bg
    };

    private String[] names = new String[] {
            "测试一","测试二",
            "测试一","测试二",
            "测试一","测试二",
            "测试一","测试二",
            "测试一","测试二",
    };
    private ListView mListView;
    private MyListAdapter mMyListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 头部
        View headerView = View.inflate(this,R.layout.listview_header_layout,null);

        mSlider = (SliderLayout) headerView.findViewById(R.id.home_slider_ad);
        mIndicator =(PagerIndicator)headerView.findViewById(R.id.home_indicator_ad);
        mGridView = headerView.findViewById(R.id.gv_grid);
        mListView = findViewById(R.id.lv_content);
        mListView.addHeaderView(headerView);

        getBannerData();
        initSlider();
        initListView();
        initGridView();
    }

    private List<Map<String,Object>> initGridViewData() {
        List<Map<String,Object>> listItems = new ArrayList<>();
        for (int i = 0; i < icons.length; i++) {
            // 如果要加载网络图片 BitmapHelper.getBitmap("http://192.168.1.119:8080/strutsproj/upload/type1.jpg")
            Map<String, Object> listItem = new HashMap<>();
            listItem.put("icon",icons[i]);
            listItem.put("name",names[i]);
            listItems.add(listItem);
        }
        return  listItems;
    }

    private  void  initGridView() {
        SimpleAdapter mSimpleAdapter = new SimpleAdapter(
                this,
                initGridViewData(),
                R.layout.gridview_item_layout,
                new String[]{"icon","name"},
                new int[]{R.id.iv_image, R.id.tv_itemname}
        );

        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mGridView.getLayoutParams();
        //params.height = 350; // 像素要转换成dp才行
        //mGridView.setLayoutParams(params);

        mSimpleAdapter.setViewBinder(new SimpleAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, Object data,
                                        String textRepresentation) {
                //判断是否为我们要处理的对象
                if(view instanceof ImageView && data instanceof Bitmap){
                    ImageView iv = (ImageView) view;
                    iv.setImageBitmap((Bitmap) data);
                    return true;
                }else
                    return false;
            }
        });

        mGridView.setAdapter(mSimpleAdapter);

        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Log.d(TAG, "onItemClick: " + i);
            }
        });

    }

    private List<String> initListData() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list.add("NAME" + i);
        }
        return list;
    }

    private void initListView() {
        mMyListAdapter = new MyListAdapter(this);
        mListView.setAdapter(mMyListAdapter);

        mMyListAdapter.addList(initListData());
        mMyListAdapter.notifyDataSetChanged();

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Log.d(TAG, "onItemClick: " + i + "   " + l);
            }
        });
    }


    private void getBannerData() {
        BannerInfo bannerInfo_01 = new BannerInfo();
        bannerInfo_01.setName("音箱狂欢");
        bannerInfo_01.setImgUrl("http://7mno4h.com2.z0.glb.qiniucdn.com/5608f3b5Nc8d90151.jpg");
        BannerInfo bannerInfo_02 = new BannerInfo();
        bannerInfo_02.setName("手机国庆礼");
        bannerInfo_02.setImgUrl("http://7mno4h.com2.z0.glb.qiniucdn.com/5608eb8cN9b9a0a39.jpg");
        BannerInfo bannerInfo_03 = new BannerInfo();
        bannerInfo_03.setName("IT生活");
        bannerInfo_03.setImgUrl("http://7mno4h.com2.z0.glb.qiniucdn.com/5608cae6Nbb1a39f9.jpg");
        listBanner.add(bannerInfo_01);
        listBanner.add(bannerInfo_02);
        listBanner.add(bannerInfo_03);
    }


    private void initSlider() {

        if (listBanner != null) {
            for (BannerInfo bannerInfo : listBanner) {
                TextSliderView textSliderView = new TextSliderView(this);
                textSliderView.image(bannerInfo.getImgUrl())
                        .description(bannerInfo.getName())
                        .setScaleType(BaseSliderView.ScaleType.CenterCrop)
                        .setOnSliderClickListener(this);
                mSlider.addSlider(textSliderView);
            }
        }

        mSlider.setDuration(3000);
        mSlider.setCustomIndicator(mIndicator);
        mSlider.setCustomAnimation(new DescriptionAnimation());
        //mSlider.setPresetTransformer(SliderLayout.Transformer.RotateUp);

        mSlider.addOnPageChangeListener(new ViewPagerEx.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.d(TAG, "onPageSelected: " + position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    // 监听广告位点击
    @Override
    public void onSliderClick(BaseSliderView slider) {
        Log.d(TAG, "onSliderClick: " + slider.getUrl() + mSlider.getCurrentPosition());

    }

    @Override
    public void onStop() {
        mSlider.stopAutoCycle();
        super.onStop();
    }

}
